<?php

namespace App\Admin\Extensions\Excel;

use App\Models\Category;
use App\Models\MemberDoctor;
use Dcat\Admin\Grid\Exporters\AbstractExporter;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;


class MedicalExporter extends AbstractExporter implements WithMapping, WithHeadings, FromCollection
{
    use Exportable;
    protected $fileName = '病例导出';
    protected $titles = [];
    // 图片数据
    protected $imageData;

    public function __construct()
    {
        $this->fileName = $this->fileName . date('Y-m-d H:i:s', time()) . '_' . Str::random(6) . '.xlsx'; //拼接下载文件名称
        $this->titles = ['id' => 'ID', 'user_id' => '真实姓名', 'agent' => '代理人', 'atm_coding' => '编码', 'status' => '状态', 'created_at' => '添加时间'];
        parent::__construct();
    }

    public function export()
    {
        // TODO: Implement export() method.
        $this->download($this->fileName)->prepare(request())->send();
        exit;
    }

    public function collection()
    {
        // TODO: Implement collection() method.
        return collect($this->buildData());
    }

    public function headings(): array
    {
        // TODO: Implement headings() method.
        return $this->titles();
    }

    public function map($row): array
    {

        // TODO: Implement map() method.
        return [
            $row['id'],
            $this->getMemberRealName($row['user_id']),
            $this->getMemberAgent($row['user_id']),
            $row['atm_coding'],
            $this->getMedicalStatus($row['status']),
            $row['created_at'],
        ];
    }

    public function getMemberRealName($userid)
    {
        return MemberDoctor::where('user_id', $userid)->first()->real_name ?? '';
    }

    public function getMemberAgent($userid)
    {
        return MemberDoctor::where('user_id', $userid)->first()->agent ?? '';
    }

    public function getMedicalStatus($status)
    {
        $state = ['未审核', '已审核', '驳回'];
        return $state[$status];
    }
}
